﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Crawler
{
    class Program
    {
        static HttpClient client = new HttpClient();

        static async Task Main(string[] args)
        {
            // 添加User-Agent头
            AddDefaultHeaders(client);

            Console.Write("请输入要搜索的关键词：");
            string keyword = Console.ReadLine();

            List<string> urls = await Search(keyword);
            HashSet<string> phoneNumbers = new HashSet<string>();

            // 并行处理每个URL
            Parallel.ForEach(urls, url =>
            {
                Console.WriteLine($"正在处理 {url} ...");
                string html = client.GetStringAsync(url).Result;
                foreach (Match match in Regex.Matches(html, @"(?<!\d)(?:\d{3}-)?\d{8}(?!\d)"))
                {
                    string phoneNumber = match.Value;
                    if (!phoneNumbers.Contains(phoneNumber))
                    {
                        Console.WriteLine($"找到 {phoneNumber}，所属URL：{url}");
                        phoneNumbers.Add(phoneNumber);
                    }
                }
            });

            Console.WriteLine($"共找到 {phoneNumbers.Count} 个电话号码。");
            Console.ReadKey();
        }

        static async Task<List<string>> Search(string keyword)
        {
            //string baiduUrl = $"https://www.baidu.com/s?wd={keyword}";
            string bingUrl = $"https://www.bing.com/search?q={keyword}";
            List<Task<string>> tasks = new List<Task<string>>();
            //tasks.Add(client.GetStringAsync(baiduUrl));
            tasks.Add(client.GetStringAsync(bingUrl));
            string[] htmls = await Task.WhenAll(tasks);
            List<string> urls = new List<string>();
            foreach (string html in htmls)
            {
                foreach (Match match in Regex.Matches(html, @"<a href=""(https?://[\w./?%&=]*)"""))
                {
                    urls.Add(match.Groups[1].Value);
                }
            }
            return urls;
        }

        static void AddDefaultHeaders(HttpClient client)
        {
            client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
        }
    }
}
